﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBOSS.Business.ReportsModule;
using System.Data;
using System.IO;

namespace RBOSS.WebUI
{
    public partial class ReportView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    Helper hlpr = new Helper();                    

                    if (Request.QueryString["FormType"].Equals("Invoice"))
                    {
                        DataTable dt = hlpr.GetInvoiceReceipt(int.Parse(Request.QueryString["OrderID"]));
                        ShowInvoice(dt);
                    }
                    else if (Request.QueryString["FormType"].Equals("Receipt"))
                    {
                        DataTable dt = hlpr.GetInvoiceReceipt(int.Parse(Request.QueryString["OrderID"]));
                        ShowReceipt(dt);
                    }
                    else if (Request.QueryString["FormType"].Equals("TopVendors"))
                    {
                        DataTable dt = hlpr.getTopProfitByVendors(DateTime.Parse(Request.QueryString["StartDate"]), (DateTime.Parse(Request.QueryString["EndDate"])));
                        ShowVendors(dt);
                    }
                    else if (Request.QueryString["FormType"].Equals("LeastVendors"))
                    {
                        DataTable dt = hlpr.getLeastProfitByVendors(DateTime.Parse(Request.QueryString["StartDate"]), (DateTime.Parse(Request.QueryString["EndDate"])));
                        ShowVendors(dt);
                    }
                    else if (Request.QueryString["FormType"].Equals("TopProducts"))
                    {
                        DataTable dt = hlpr.getTopProducts(DateTime.Parse(Request.QueryString["StartDate"]), (DateTime.Parse(Request.QueryString["EndDate"])), int.Parse(Request.QueryString["CategoryId"]));
                        ShowProducts(dt);
                    }
                    else if (Request.QueryString["FormType"].Equals("LeastProducts"))
                    {
                        DataTable dt = hlpr.getLeastProducts(DateTime.Parse(Request.QueryString["StartDate"]), (DateTime.Parse(Request.QueryString["EndDate"])), int.Parse(Request.QueryString["CategoryId"]));
                        ShowProducts(dt);
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                } 
            }
        }

        private void ShowInvoice(DataTable dt)
        {
            Invoice inv = new Invoice();

            this.SetTextObject(inv, "txtName", dt.Rows[0]["CustomerName"].ToString());
            this.SetTextObject(inv, "txtInvoiceNO", "INV" + dt.Rows[0]["OrderId"].ToString());
            this.SetTextObject(inv, "txtDate", DateTime.Parse(dt.Rows[0]["OrderDate"].ToString()).ToString("MM/dd/yyyy"));
            this.SetTextObject(inv, "txtCustomerID", dt.Rows[0]["UserId"].ToString());

            inv.SetDataSource(dt);

            MemoryStream ms = (MemoryStream)inv.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(ms.ToArray());

            ms.Flush();
            ms.Close();
            ms.Dispose();

            Response.Flush();
            Response.End();
        }

        private void ShowReceipt(DataTable dt)
        {
            Receipt rec = new Receipt();

            this.SetTextObject(rec, "txtName", dt.Rows[0]["CustomerName"].ToString());
            this.SetTextObject(rec, "txtReceiptNO", "RCIPT" + dt.Rows[0]["OrderId"].ToString());
            this.SetTextObject(rec, "txtDate", DateTime.Parse(dt.Rows[0]["OrderDate"].ToString()).ToString("MM/dd/yyyy"));
            this.SetTextObject(rec, "txtCustomerID", dt.Rows[0]["UserId"].ToString());
            this.SetTextObject(rec, "txtRecName", dt.Rows[0]["CustomerName"].ToString());
            this.SetTextObject(rec, "txtRecNO", "RCIPT" + dt.Rows[0]["OrderId"].ToString());
            this.SetTextObject(rec, "txtRecDate", DateTime.Parse(dt.Rows[0]["OrderDate"].ToString()).ToString("MM/dd/yyyy"));

            rec.SetDataSource(dt);

            MemoryStream ms = (MemoryStream)rec.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(ms.ToArray());

            ms.Flush();
            ms.Close();
            ms.Dispose();

            Response.Flush();
            Response.End();
        }

        private void ShowVendors(DataTable dt)
        {
            dt = dt.AsEnumerable().Take(5).CopyToDataTable();
            VendorsRep vp = new VendorsRep();

            this.SetTextObject(vp, "txtStartDate", Request.QueryString["StartDate"]);
            this.SetTextObject(vp, "txtEndDate", Request.QueryString["EndDate"]);

            if (Request.QueryString["FormType"].Equals("TopVendors"))
                this.SetTextObject(vp, "txtChartTitle", "Top 5 Vendors");
            else
                this.SetTextObject(vp, "txtChartTitle", "Least 5 Vendors");

            vp.SetDataSource(dt);

            MemoryStream ms = (MemoryStream)vp.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(ms.ToArray());

            ms.Flush();
            ms.Close();
            ms.Dispose();

            Response.Flush();
            Response.End();
        }

        private void ShowProducts(DataTable dt)
        {
            dt = dt.AsEnumerable().Take(5).CopyToDataTable();
            ProductsRep pr = new ProductsRep();

            this.SetTextObject(pr, "txtStartDate", Request.QueryString["StartDate"]);
            this.SetTextObject(pr, "txtEndDate", Request.QueryString["EndDate"]);
            this.SetTextObject(pr, "txtCategoryName", Request.QueryString["CategoryName"]);

            if (Request.QueryString["FormType"].Equals("TopProducts"))
                this.SetTextObject(pr, "txtChartTitle", "Best Selling Products");
            else
                this.SetTextObject(pr, "txtChartTitle", "Least Selling Products");

            pr.SetDataSource(dt);

            MemoryStream ms = (MemoryStream)pr.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(ms.ToArray());

            ms.Flush();
            ms.Close();
            ms.Dispose();

            Response.Flush();
            Response.End();
        }

        private void SetTextObject(Invoice inv, string objectName, string text)
        {
            CrystalDecisions.CrystalReports.Engine.TextObject textObject =
                inv.ReportDefinition.ReportObjects[objectName] as CrystalDecisions.CrystalReports.Engine.TextObject;
            if (textObject != null)
            {
                textObject.Text = text;
            }
        }

        private void SetTextObject(Receipt rec, string objectName, string text)
        {
            CrystalDecisions.CrystalReports.Engine.TextObject textObject =
                rec.ReportDefinition.ReportObjects[objectName] as CrystalDecisions.CrystalReports.Engine.TextObject;
            if (textObject != null)
            {
                textObject.Text = text;
            }
        }

        private void SetTextObject(VendorsRep vp, string objectName, string text)
        {
            CrystalDecisions.CrystalReports.Engine.TextObject textObject =
                vp.ReportDefinition.ReportObjects[objectName] as CrystalDecisions.CrystalReports.Engine.TextObject;
            if (textObject != null)
            {
                textObject.Text = text;
            }
        }

        private void SetTextObject(ProductsRep pr, string objectName, string text)
        {
            CrystalDecisions.CrystalReports.Engine.TextObject textObject =
                pr.ReportDefinition.ReportObjects[objectName] as CrystalDecisions.CrystalReports.Engine.TextObject;
            if (textObject != null)
            {
                textObject.Text = text;
            }
        }
    }
}